package com.yld.user.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.yld.base.dao.impl.BaseDaoImpl;
import com.yld.user.dao.UserDao;
import com.yld.user.entity.UserEntity;
import com.yld.user.utils.DBUtil;

public class UserDaoImpl extends BaseDaoImpl<UserEntity> implements UserDao {
	
	

	@Override
	public void addUser(UserEntity entity) throws Exception {
		
		
		//往oracle数据库  OA_USER_T 中插入1条记录
		
		
		//1 连接oracle数据库
		Connection connection = DBUtil.getConnection();
		
		
		//2 准备sql 占位符
		String sql ="INSERT INTO OA_USER_T(ID,USERNAME,PASSWORD,AGE) VALUES(OA_USER_S.NEXTVAL,?,?,?)";
		
		//3把字符串封装成可执行的sql命令    准备好的执行语句
		PreparedStatement ps = connection.prepareStatement(sql);
		
		
		//4对参数给定具体的值
		
		//ps.setInt(1, entity.getId());
		ps.setString(1, entity.getUsername());
		ps.setString(2, entity.getPassword());
		ps.setInt(3, entity.getAge());
		
		//5执行命令
		ps.executeUpdate();
		

	}

	@Override
	public void updateUser(Integer id, Integer newAge) throws Exception {
		
		//1 连接oracle数据库
		Connection connection = DBUtil.getConnection();
		
		
		//2 准备sql 占位符
		String sql ="UPDATE OA_USER_T SET  AGE=? WHERE ID=?";
		
		//3把字符串封装成可执行的sql命令    准备好的执行语句
		PreparedStatement ps = connection.prepareStatement(sql);
		
		
		//4对参数给定具体的值
		
		ps.setInt(1, newAge);
		ps.setInt(2, id);
		
		//5执行命令
		ps.executeUpdate();
		
		ps.close();
		connection.close();
		
		
	}

	@Override
	public UserEntity findUserById(Integer id) throws Exception{
		
		
		
		UserEntity  user=new UserEntity();
		//1 连接oracle数据库
		Connection connection = DBUtil.getConnection();
		
		
		//2 准备sql 占位符
		String sql ="SELECT * FROM OA_USER_T WHERE ID=?";
		
		//3把字符串封装成可执行的sql命令    准备好的执行语句
		PreparedStatement ps = connection.prepareStatement(sql);
		
		
		//4对参数给定具体的值
		ps.setInt(1, id);
		
		//5执行命令
		ResultSet rs = ps.executeQuery();
		
		
		//? 判断结果集中有没有数据  有多少数据?
	
		if(rs.next()){
			System.out.println("有数据....");
			
			//从 rs 拿到我们想要的数据
			
			
			Integer ic  = rs.getInt(1);
			String username  = rs.getString(2);
			String password  = rs.getString(3);
			Integer age  = rs.getInt(4);
			
			
			user.setId(ic);
			user.setUsername(username);
			user.setPassword(password);
			user.setAge(age);
			
		}else {
			System.out.println("无....");
			
		}
		
		return user;
		
		
	}

	@Override
	public List<UserEntity> findAll() throws Exception {
		
		
		List<UserEntity> list = new ArrayList<UserEntity>();
		
		//1 连接oracle数据库
		Connection connection = DBUtil.getConnection();
		
		
		//2 准备sql 占位符
		String sql ="SELECT * FROM OA_USER_T";
		
		//3把字符串封装成可执行的sql命令    准备好的执行语句
		PreparedStatement ps = connection.prepareStatement(sql);
		
		
		//4对参数给定具体的值
		//ps.setInt(1, id);
		
		//5执行命令
		ResultSet rs = ps.executeQuery();
		
		
		//? 判断结果集中有没有数据  有多少数据?
	
		while(rs.next()){
			//System.out.println("有数据....");
			
			//从 rs 拿到我们想要的数据
			
			
			Integer ic  = rs.getInt(1);
			String username  = rs.getString(2);
			String password  = rs.getString(3);
			Integer age  = rs.getInt(4);
			
			UserEntity  user=new UserEntity();
			user.setId(ic);
			user.setUsername(username);
			user.setPassword(password);
			user.setAge(age);
			
			list.add(user);
			
		}
		
		return list;
	}


}
